/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: AbstractRewardProcessor
 * Author:   zhouheng
 * Date:     2019/4/14 23:36
 * Description: 领奖抽象类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.activity.services.processor;

import com.activity.dal.entitys.ActDrawAward;
import com.activity.dal.entitys.ActDrawRecord;
import com.activity.dal.persistence.ActDrawRecordMapper;
import com.activity.services.processor.constants.DrawContants;
import com.activity.services.processor.exception.UnRewardException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * 〈一句话功能简述〉<br> 
 * 〈领奖抽象类〉
 *
 * @author zhouheng
 * @create 2019/4/14
 * @since 1.0.0
 */
public abstract class AbstractRewardProcessor implements RewardProcessor<ActivityDrawContext> {

    @Autowired
    protected RedisTemplate redisTemplate;

    @Autowired
    ActDrawRecordMapper actDrawRecordMapper;

    /**
     * @description: 验证奖品数量及获取奖品明细
     * @date: 2019/4/14 23:59
     * @author:zhouheng
     * @param activityDrawContext
     */
    private void beforeProcessor(ActivityDrawContext activityDrawContext) {
        //判断奖项当天发放总数以及整个活动过程中的总数：total_num、day_total_num
        if(validDayAwardNum(activityDrawContext) && validTotalAwardNum(activityDrawContext)){
            ActDrawAward actDrawAward = (ActDrawAward) redisTemplate.opsForValue().get(DrawContants.DRAW_ITEM + getAwardType()+":"
                    + activityDrawContext.getActDrawAwardItem().getId());

            //TODO 判断奖品的状态
            activityDrawContext.setActDrawAward(actDrawAward);
        }else{
            throw new UnRewardException("当前奖项已达到当日最大限制");
        }

    }

    public abstract int getAwardType();


    /**
     * @description: 领奖业务
     * @date: 2019/4/14 23:57
     * @author:zhouheng
     */
    public ActivityDrawContext doReword(ActivityDrawContext activityDrawContext) {
        // 1. 验证奖品数量及获取奖品明细
        beforeProcessor(activityDrawContext);
        // 2. 处理发放奖品
        processor(activityDrawContext);
        // 3. 发送短信
        afterProcessor(activityDrawContext);

        return null;
    }

    /**
     * @description: 发送短信
     * @date: 2019/4/15 0:01
     * @author:zhouheng
     * @param activityDrawContext
     */
    public abstract  void afterProcessor(ActivityDrawContext activityDrawContext);

    /**
     * @description: 处理发放奖品
     * @date: 2019/4/14 23:59
     * @author:zhouheng
     * @param activityDrawContext
     */
    public abstract void processor(ActivityDrawContext activityDrawContext);


   /**
    * @description: 校验每日奖品数量
    * @date: 2019/4/15 7:33
    * @author:zhouheng
    */
    protected boolean validDayAwardNum(ActivityDrawContext activityDrawContext) {
        //TODO 判断当天针对当前奖项发放的数量
        return true;
    }

    /**
     * @description: 校验奖品数量
     * @date: 2019/4/15 7:34
     * @author:zhouheng
     */
    protected boolean validTotalAwardNum(ActivityDrawContext activityDrawContext) {
        //TODO 判断当前奖项总的发放数量
        return true;
    }

    /**
     * @description: 保存中奖记录
     * @date: 2019/4/15 7:58
     * @author:zhouheng
     */
    protected void saveAwardRecord(ActivityDrawContext activityDrawContext) {
        ActDrawRecord actDrawRecord = new ActDrawRecord();
        actDrawRecord.setAwardName(activityDrawContext.getActDrawAward().getAwardName());
        actDrawRecord.setLevel(activityDrawContext.getActDrawAwardItem().getLevel());
        actDrawRecord.setMobile(activityDrawContext.getCurrentUser().getMobile());
        actDrawRecord.setUid(activityDrawContext.getActivityTurntableDrawReq().getUid());
        actDrawRecord.setName(activityDrawContext.getCurrentUser().getRealName());
        actDrawRecordMapper.addActDrawRecord(actDrawRecord);
    }
}